<?php
/**
 * @package Autogenerator for Joomla!
 * @version 3.0.0
 * @author Truong Cong Duc <truongcongduc4@gmail.com>
 * @link autogenerator.blogspot.com
 * @copyright (c) 2013, Truong Cong Duc
 * @license GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('_JEXEC') or die;

class AutogeneratorControllerMvc extends JControllerForm {

    public function __construct($config = array()) {
        parent::__construct($config);
    }

    public function exportPHPFile() {
        $oApp = JFactory::getApplication();
        $sFileName = $oApp->input->get('file', 'index.php');
        $sType = $oApp->input->get('type', 'list');
        $bIsAdmin = $oApp->input->getBool('bIsAdmin', true);
        $iMvcId = $oApp->input->getInt('id');
        $oMvc = $this->getModel()->getItem($iMvcId);
        $sExportFile = $sFileName;
        switch ($sFileName) {
            case 'view.html.php':
                if ($bIsAdmin) {
                    switch ($sType) {
                        case 'list':
                            $sContent = AutogeneratorHelper::getAdminListViewHTMLContent($iMvcId);
                            break;

                        case 'form':
                            $sContent = AutogeneratorHelper::getAdminFormViewHTMLContent($iMvcId);
                            break;

                        default:
                            break;
                    }
                } else {
                    switch ($sType) {
                        case 'list':
                            $sContent = AutogeneratorHelper::getFrontendListViewHTMLContent($iMvcId);
                            break;

                        case 'item':
                            $sContent = AutogeneratorHelper::getFrontendItemViewHTMLContent($iMvcId);
                            break;

                        default:
                            break;
                    }
                }
                $sExportFile = 'view.html.php';
                break;
            case 'default.php':
                if ($bIsAdmin) {
                    $sContent = AutogeneratorHelper::getAdminListDefaultContent($iMvcId);
                } else {
                    $sContent = AutogeneratorHelper::getFrontendItemDefaultContent($iMvcId);
                }


                break;

            case 'model.php':
                if ($bIsAdmin) {
                    $sContent = AutogeneratorHelper::getAdminModelContent($iMvcId);
                } else {
                    $sContent = AutogeneratorHelper::getFrontendModelContent($iMvcId);
                }
                $sExportFile = ($oMvc->id > 0 ? $oMvc->title . '.php' : 'index.php');
                break;

            case 'edit.php':
                $sContent = AutogeneratorHelper::getAdminFormEditContent($iMvcId);
                break;
                
            case 'models.php':
                if ($bIsAdmin) {
                    $sContent = AutogeneratorHelper::getAdminModelsContent($iMvcId);
                } else {
                    $sContent = AutogeneratorHelper::getFrontendModelsContent($iMvcId);
                }
                $sExportFile = ($oMvc->id > 0 ? $oMvc->title . '.php' : 'index.php');
                break;

            case 'form.xml':
                if ($bIsAdmin) {
                    $sContent = AutogeneratorHelper::getAdminXMLFormContent($iMvcId);
                } else {
                    
                }
                $sExportFile = ($oMvc->id > 0 ? $oMvc->title . '.xml' : 'form.xml');
                break;

            case 'controller.php':
                if ($bIsAdmin) {
                    $sContent = AutogeneratorHelper::getAdminControllerContent($iMvcId);
                } else {
                    $sContent = AutogeneratorHelper::getFrontendControllerContent($iMvcId);
                }
                $sExportFile = ($oMvc->id > 0 ? $oMvc->title . '.php' : 'controller.php');
                break;
            case 'controllers.php':
                if ($bIsAdmin) {
                    $sContent = AutogeneratorHelper::getAdminControllersContent($iMvcId);
                } else {
                    $sContent = AutogeneratorHelper::getFrontendControllersContent($iMvcId);
                }
                $sExportFile = ($oMvc->id > 0 ? $oMvc->title . '.php' : 'controllers.php');
                break;
            default:
                break;
        }

        //$sContent = AutogeneratorHelper::getContentFileInTemplate($sFileName, $oApp->input->get('type', 'list'), $oApp->input->getBool('bIsAdmin', true));
        // Set headers
        header("Cache-Control: public");
        header("Content-Description: File Transfer");
        header("Content-Disposition: attachment; filename=$sExportFile");
        header("Content-Type: application/zip");
        header("Content-Transfer-Encoding: binary");

        echo $sContent;
        die;
    }

}
